'use strict';

var nmSrv = require('../appnet.link-server');
var nmCln = nmSrv.Client;
var SEP = nmSrv.SEP;

var dgram = require("dgram");

// appnet.link-ws library
var WebSocket = require('wspp').wspp;
var WebSocketServer = WebSocket.Server;

// msgpack library
var msgpack = require('msgpack-js');

/////////////////////////////////////////////////////////////////
// file share App
var express = require('express'),
    app = express();

app.use(express.directory(__dirname + '/shareA'));
app.use(express.static(__dirname + '/shareA'));
/////////////////////////////////////////////////////////////////

// create websocket server with name-client
var creatNmclnWss = function(self) {
    var wss = new WebSocketServer({httpp: true, server: self.bsrv.srv, path: SEP.SEP_CTRLPATH_BS});
    
    wss.on('connection', function(client){    
        console.log('new ws connection: ' +
                    client._socket.remoteAddress+':'+client._socket.remotePort+' -> ' + 
                    client._socket.address().address+':'+client._socket.address().port);
                                
        client.on('message', function(message) {
            var data = (typeof message !== 'string') ? msgpack.decode(message) : JSON.parse(message);
            ///console.log('business message:'+JSON.stringify(data));
            data += 'reply';
    
            try {
                client.send(msgpack.encode(data), function(err){
                    if (err) {
                        console.log(err+',sendOpcMsg failed');
                    }
                });
            } catch (e) {
                console.log(e+',sendOpcMsg failed immediately');
            }
        });
    });
}

// clients A
var nmclnsA = new nmCln({
    srvinfo: {
        timeout: 20,
        endpoints: [{ip: 'localhost', port: 51686}, {ip: 'localhost', port: 51868}],
        turn: [
            {ip: 'localhost', agent: 51866, proxy: 51688} // every turn-server include proxy and agent port
        ]
    },
    usrinfo: {domain: '51dese.com', usrkey: 'A'},
    conmode: SEP.SEP_MODE_CS // c/s mode as httpp server
});

nmclnsA.on('ready', function(){
    console.log('name-nmclnsA ready');
    
    // create websocket server
    creatNmclnWss(this);
});

// dummy http server
var http = require('http');

http.createServer(app).listen(51688);
console.log('file share http server listening on 51688');
